#include "task.h"
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

/* Lunghezza dell'iperperiodo */
#define H_PERIOD_ 20

/* Numero di frame */
#define NUM_FRAMES_ 5

//lunghezza frame 4 unità da 10 millisecondi

/* Numero di task */
#define NUM_TASKS_ 2
#include <boost/config/posix_features.hpp>

void task0_code();
void task1_code();
/**********************/

/* Questo inizializza i dati globali */
const unsigned int H_PERIOD = H_PERIOD_;
const unsigned int NUM_FRAMES = NUM_FRAMES_;
const unsigned int NUM_TASKS = NUM_TASKS_;

task_routine TASKS[NUM_TASKS_];
int * SCHEDULE[NUM_FRAMES_];

void task_init()
  {
  /* Inizializzazione di TASKS[] */
  TASKS[0] = task0_code;
  TASKS[1] = task1_code;


  /* Inizializzazione di SCHEDULE */

  /* frame 0 */
  SCHEDULE[0] = (int *) malloc( sizeof( int ) * 3 );
  SCHEDULE[0][0] = 0;
  SCHEDULE[0][1] = 1;
  SCHEDULE[0][2] = -1;

  /* frame 1 */
  SCHEDULE[1] = (int *) malloc( sizeof( int ) * 2 );
  SCHEDULE[1][0] = 0;
  SCHEDULE[1][1] = -1;

  /* frame 2 */
  SCHEDULE[2] = (int *) malloc( sizeof( int ) * 1 );
  SCHEDULE[2][0] = -1;

  /* frame 3 */
  SCHEDULE[3] = (int *) malloc( sizeof( int ) * 2 );
  SCHEDULE[3][0] = 1;
  SCHEDULE[3][1] = -1;

  /* frame 4 */
  SCHEDULE[4] = (int *) malloc( sizeof( int ) * 1 );
  SCHEDULE[4][0] = -1;

  /* Custom Code */
  }

void task_destroy()
  {
    unsigned int i;

  /* Custom Code */

  for ( i = 0; i < NUM_FRAMES; ++i )
    free( SCHEDULE[i] );
  }


 
double gettime(){
	struct timeval tmp;
	gettimeofday(&tmp,NULL);
	return 1.0*tmp.tv_sec+1e-6* tmp.tv_usec;

}

//ogni quanto sono 10 ms*/
void busywait(int millisec)
{
	double start;
	double stop;
	start= gettime();
	stop = start + 1e-3*millisec;
	while (gettime()<stop);
}

void task0_code()
   {
   /* Custom Code */
    busywait (60);
  }

void task1_code()
   {
   /* Custom Code */
   
    busywait (50);
  }

  

